package com.ithm.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ithm.reggie.common.R;
import com.ithm.reggie.dto.SetmealDto;
import com.ithm.reggie.entity.Category;
import com.ithm.reggie.entity.Setmeal;
import com.ithm.reggie.service.CategoryService;
import com.ithm.reggie.service.SetmealService;
import com.ithm.reggie.service.impl.SetmealDishServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishServiceImpl setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 保存套餐信息到setmeal表和setmeal_dish表中
     */
    @PostMapping
    public R<String> getSetmealByDishId(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息{}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("套餐信息保存成功");
    }

    /**
     * 套餐分页查询
     */
    @GetMapping("/page")
    public R<Page> getSetmealPage(int page, int pageSize, String name){
        //构造分页构造器对象
        Page<SetmealDto> setmealDtoPage=new Page<>(page,pageSize);
        Page<Setmeal> pageinfo=new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        //添加过滤条件
        lambdaQueryWrapper.like(name!=null,Setmeal::getName,name);
        //只展示未被删除的数据
        lambdaQueryWrapper.eq(Setmeal::getIsDeleted,0);
        //添加排序条件
        lambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageinfo,lambdaQueryWrapper);


        //处理records字段都拷贝
        BeanUtils.copyProperties(pageinfo,setmealDtoPage,"records");

        List<Setmeal> records = pageinfo.getRecords();
        List<SetmealDto> list = records.stream().map((item -> {
            SetmealDto setmealDto = new SetmealDto();

            //拷贝除菜名其它字段
            BeanUtils.copyProperties(item, setmealDto);

            //查询菜名
            Long id = item.getCategoryId();
            Category category = categoryService.getById(id);
            if (category!=null){
                //添加菜名
                String categoryname = category.getName();
                setmealDto.setCategoryName(categoryname);
            }
            return setmealDto;
        })).collect(Collectors.toList());

        setmealDtoPage.setRecords(list);

        return R.success(setmealDtoPage);
    }

    /**
     * 删除套餐，同时删除相应setmeal_dish表中的数据
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("id:{}",ids);
        setmealService.removeWithDish(ids);
        return R.success("删除套餐成功");
    }

    /**
     * 停售、起售
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable int status, @RequestParam List<Long> ids) {
        log.info("status:{},ids:{}",status,ids);

        LambdaUpdateWrapper<Setmeal> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.in(Setmeal::getId, ids).set(Setmeal::getStatus, status);

        setmealService.update(lambdaUpdateWrapper);

        return R.success("菜品销售状态更新成功");
    }

    /**
     * 修改套餐，同时setmeal_dish表也做修改
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());
        setmealService.updateWithDish(setmealDto);
        return R.success("修改菜品成功");
    }

    /**
     * 修改套餐之前先回显数据
     */
    @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable Long id){
        log.info("id:{}",id);
        SetmealDto setmealDto = setmealService.getByIdWithDish(id);
        return R.success(setmealDto);
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);

        return R.success(list);
    }
}
